<?php
	session_start();
	
	if(isset($_POST["schemename"])){
		include("write_functions.php");
		connect("localhost", "root", "", "gradegriddatabase");
		
		$username = $_SESSION["username"];
		$accountid_query = mysql_query("SELECT account_id FROM account WHERE username = '$username'");
		$accountid_array = mysql_fetch_array($accountid_query);
		$accountid = $accountid_array["account_id"];
		$schemename = mysql_real_escape_string($_POST["schemename"]);
		createscheme($schemename, $accountid);
		
		$schemeid_query = mysql_query("SELECT scheme_id FROM scheme WHERE scheme_name = '$schemename'");
		$schemeid_array = mysql_fetch_array($schemeid_query);
		$schemeid = $schemeid_array["scheme_id"];
		
		$limit = $_POST["limit"];
		$i = 0;
		$prevend = "";
		
		while($i <= $limit){
			$lowbound = $_POST["lb" . $i];
			$upbound = $_POST["ub" . $i];
			$label = $_POST["bracket" . $i];
			$lowcond = $_POST["lowercond" . $i];
			
			if($i == 0 || $prevend == '('){
				createblock($schemeid, $lowbound, $upbound, $label, $lowcond, ']');
			} else{
				createblock($schemeid, $lowbound, $upbound, $label, $lowcond, ')');
			}
			
			$prevend = $lowcond;
			$i++;
		}
	}
?>
<html>
	<head>
		<title>Create a New Grading Scheme</title>
		<style type="text/css">
			p, .pMirror{
				font-family: "Trebuchet MS", sans-serif;
				font-size: 13px;
				text-align: justify;
				color: #000000;
				margin-right: 8px;
			}
		
			h1{
				font-family: Arial, Helvetica, sans-serif;
				font-size: 18px;
				color: #000000;
			}
			
			.form_section{
				background-color: #DADADA;
				-moz-border-radius: 5px;
				-webkit-border-radius: 5px;
				border-radius: 5px;
				padding: 0px 4px;
				width: 100%;
				min-width: 30%;
			}
		</style>
		<script type="text/javascript" language="javascript">
			var bracket_runner = 0
			
			function addBrackets(){
				var lowerstate = document.getElementById("inc" + bracket_runner)
				bracket_runner++
				var table = document.getElementById("brackets")
				var labelrow = table.insertRow(table.rows.length)
				var labelcell = labelrow.insertCell(0)
				labelcell.colSpan = 2
				labelcell.innerHTML = "Bracket label: <input type='text' name='bracket" + bracket_runner + "' />"
				
				var boundrow = table.insertRow(table.rows.length)
				var lowboundcell = boundrow.insertCell(0)
				lowboundcell.innerHTML = 'Lower bound: <input type="text" name="lb' + bracket_runner + '" /><br />' +
				                         '<input type="radio" id="inc' + bracket_runner + '" name="lowercond' + bracket_runner + '" value="[" />' +
				                         '<label for="inc' + bracket_runner + '">Inclusive</label><br />' +
				                         '<input type="radio" checked="checked" id="exc' + bracket_runner + '" name="lowercond' + bracket_runner + '" value="(" />' +
				                         '<label for="exc' + bracket_runner + '">Exclusive</label><br />'
				
				var highboundcell = boundrow.insertCell(0)
				highboundcell.vAlign = "top"
				highboundcell.innerHTML = 'Upper bound: <input type="text" name="ub' + bracket_runner + '" /><br />'
			}
			
			function save(){
				document.getElementById("limit").value = bracket_runner;
				document.getElementById("newscheme").submit()
				alert("Grading Scheme saved.")
			}
		</script>
	</head>
	<body>
		<div class="form_section"><h1>Create a New Grading Scheme</h1></div>
		<form action="newscheme.php" method="post" id="newscheme">
			<p>Scheme Name:<br />
			<input type="text" name="schemename" maxlength="50" /></p>
			
			<p>Brackets:</p>
			<table class="pMirror" id="brackets" border="0">
			<tr><td colspan="2">Bracket label: <input type="text" name="bracket0" /></td></tr>
			<tr><td valign="top">Upper bound: <input type="text" name="ub0" /><br /></td>
				
				<td>Lower bound: <input type="text" name="lb0" /><br />
				<input type="radio" id="inc0" name="lowercond0" value="[" />
				<label for="inc0">Inclusive</label><br />
				<input type="radio" checked="checked" id="exc0" name="lowercond0" value="(" />
				<label for="exc0">Exclusive</label><br /></td></tr>
			</table>
			<input type="text" name="limit" id="limit" value="" style="visibility: hidden;" />
			<input type="button" value="Add more brackets" onclick="addBrackets()" /> <input type="button" value="Save" onclick="save()" />
		</form>
	</body>
</html>